// pages/ucenter/index/index.js

const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
const user = require('../../../utils/user.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    integral:0,
    money:0.00,
    userInfo:{},
    modalName:'',
    nun1:0,
    nun2: 0,
    nun3: 0,
    nun4: 0,
    icon: [ {
        name: 'card',
        name1: '全部订单',
        isShow: true,
        url: '../list/list?id=0'
      },
      {
        name: 'text',
        name1: '待付款',
        isShow: true,
        url: '../list/list?id=1'
      },
      {
        name: 'deliver',
        name1: '待提货',
        isShow: true,
        url: '../list/list?id=2'
      },
      {
        name: 'read',
        name1: '已完成',
        isShow: true,
        url: '../list/list?id=3'
      }
    ],
    icon1: [{
        name: 'post',
        name1: '收货地址',
        isShow: true,
        url:'../adress/adress'
      }, {
        name: 'friendfamous',
        name1: '成为团长',
        isShow: true,
        url: '../group/group'
      },
      {
        name: 'mobile',
        name1: '绑定手机号',
        isShow: true,
        url: '../phone/phone'
      },
      {
        name: 'medalfill',
        name1: '关于我们',
        isShow: true,
        url: '../about/about'
      },
      {
        name: 'safe',
        name1: '成为供货商',
        isShow: true,
        url: '../Supplier/Supplier'
      },
       {
        name: 'group',
        name1: '完善信息',
        isShow: true,
         url: '../person/person'
      },
      {
        name: 'refund',
        name1: '积分兑换',
        isShow: true,
        url: '../integral/integral'
      }
    ],
    haslogin:false
  },

  getall() {
    let that = this
    util.request(api.QueryAllOrder, {

    }, 'POST', {
        'Content-Type': 'application/json;charset=UTF-8',
        'X-Qxmall-Token': wx.getStorageSync('token')
      }).then(function (res) {
        console.log(res)
        if (res.code == 200) {
          if (res.data.orderList.length == 0) {
            that.setData({
              has1: true
            })
          } else {
            app.globalData.allorder = res.data.orderList
            console.log(app.globalData.allorder)
            that.setData({
              nun2: 0
            })
            for (var i = 0; i < res.data.orderList.length;i++){
              if (res.data.orderList[i].status==0){
                that.setData({
                  nun2:that.data.nun2+1
                })
              }
              if (res.data.orderList[i].status == 1) {
                that.setData({
                  nun3: that.data.nun3 + 1
                })
              }
              if (res.data.orderList[i].status == 2) {
                that.setData({
                  nun4: that.data.nun4 + 1
                })
              }
            }
            console.log(that.data.nun2)
          }
        }
      });
  },

  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },

  getmoney() {
    if (this.data.haslogin == false) {
     return false
    }
    let that = this
    util.request(api.GainUserBalance, {}, 'post', {
      'Content-Type': 'application/json;charset=UTF-8',
      'X-Qxmall-Token': wx.getStorageSync('token')
    }).then(function (res) {
      console.log(res)
      if(res.code==200){
        if (res.data.userbalance==null){
          return false
        }else{
          that.setData({
            money: res.data.userbalance.balance,
            integral: res.data.userbalance.integrationNum
          })
        }
      }
     
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      haslogin: app.globalData.hasLogin,
      userInfo: wx.getStorageSync('userInfo')
    });
    if(this.data.haslogin==false){
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },
  gobalance(){
    console.log('111')
    wx.navigateTo({
      url: '../balance/balance',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.onLoad()
    this.getmoney()
    this.getall()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})